import java.util.*;
import java.awt.*;
public class Grid {
	public static final int SIZE=20;
	private int length=25;
	private int width=10;
	Graphics2D g;
	private int xCoord;
	private int yCoord;
	private Color color;
	private ArrayList<Piece> pieces;
	private int windowWidth;
	private int windowLength;
	private boolean reset=false;
	private TetrisGame game;

	public Grid(int width, int length,Color color, Graphics2D g,int windowWidth,int windowLength, TetrisGame game){
		this.width=width;
		this.game = game;
		this.length=length;
		this.g = g;
		this.color=color;
		g.setColor(color);
		this.windowWidth=windowWidth;
		this.windowLength=windowLength;
		xCoord=windowWidth/2-width*SIZE;
		yCoord=(windowLength-length*SIZE)/2;
		pieces=new ArrayList<Piece>();
	}
	
	public Grid getGrid(){
		return this;
	}
	
	public int getLength(){
		return length;
	}
	
	public int getWidth(){
		return width;
	}
	
	public int getSize(){
		return SIZE;
	}
	
	public int getXCoord(){
		return xCoord;
	}
	
	public int getYCoord(){
		return yCoord;
	}
	public ArrayList<Piece> getPieces(){
		return pieces;
	}
	
	public void paint(Graphics2D g){
		//rotate();
		g.setColor(Color.PINK);
		g.drawRect(1,-1,100,100);
		g.setColor(Color.CYAN);
		g.drawRect(-1,-1,100,100);
		g.setColor(Color.ORANGE);
		g.drawRect(-1,1,100,100);
		g.setColor(color);
		g.fillRect(xCoord,yCoord,width*SIZE,length*SIZE);
		g.setColor(Color.YELLOW);
		for(int i=yCoord;i<=length*SIZE+yCoord;i+=SIZE){
			g.drawLine(xCoord,i,width*SIZE+xCoord,i);
		}
		for(int j=xCoord;j<=width*SIZE+xCoord;j+=SIZE){
			g.drawLine(j,yCoord,j,length*SIZE+yCoord);
		}
		for(Piece p:pieces){
			p.putSelfInGrid(g);
		}
		for(int i=0;i<pieces.size();i++){
			Piece p=pieces.get(i);
			ArrayList<Block>temp=p.getBlocks();
			for(int j=0;j<temp.size();j++){
				Block b=temp.get(j);
				b.updateLoc();
			}
		}
		Piece p=pieces.get(pieces.size()-1);
		for(Block b:p.getBlocks()){
//
//			if(!isValid(b.getLoc().getLocationUnder())||isOccupied(b.getLoc().getLocationUnder())){
//				reset=true;

			if(!isValid(b.getLoc().getLocationUnder())||isOccupied(b.getLoc().getLocationUnder())){
				p.freeze();
				reset();

			}
		}
		
		if(reset){
			p.freeze();
			reset();
			p=pieces.get(pieces.size()-1);
			System.out.println(p);
			System.out.println(p.getLoc());
			if(isAtTop(p)){
				System.out.println("true");
				javax.swing.JOptionPane.showMessageDialog(null, "Score: " + game.getScore(), "GAME OVER!", 0);
				game.destroy();
			}
		}
		p.move();
	}
	public TetrisGame getGame(){
		return game;
	}
	public boolean isAtTop(Piece p){
		boolean pieceUnder=false;
		boolean rowZero=false;
		for(Block b:p.getBlocks()){
			if (isOccupied(b.getLoc().getLocationUnder())){
				pieceUnder=true;
			}
			if(b.getLoc().getRow()==0){
				rowZero=true;
			}
		}
		if(pieceUnder&&rowZero){
			return true;
		}
		return false;
	}
	public void clearRow(int rowNum){
		ArrayList<Location> locs=new ArrayList<Location>();
		for(int i=0;i<=width;i++){
			locs.add(new Location(i,rowNum,this));
		}
		for(Location loc:locs){
			Block b=loc.get();
			b.removeSelfFromGrid();
		}
	}
	public void reset(){
		System.out.println("reset");
		xCoord=windowWidth/2-width*SIZE;
		yCoord=(windowLength-length*SIZE)/2-SIZE;
		length=25;
		width=10;
		insertPiece();
		System.out.println(pieces.get(pieces.size()-1));
		game.resetCount();
		reset=false;
//=======
//		count1++;
//		System.out.println(count1+":"+pieces.get(pieces.size()-1));
//		reset=true;
//>>>>>>> .r8
	}
	
	public boolean getReset(){
		return reset;
	}
	
	public void setReset(){
		reset=false;
	}
	
	public void shift (int xShift,int yShift){
		g.setColor(color);
		xCoord+=xShift;
		yCoord+=yShift;
	}
	
	public void rotate(){
		if(game.getRotation()%Math.PI/2==0&&game.getRotation()!=0){
			yCoord*=-1;
		}else if(game.getRotation()%Math.PI==0&&game.getRotation()!=0){
			xCoord*=-1;
		}
		int temp=width;
		width=length;
		length=temp;
	}
	
	public void insertPiece(){
		int random=(int)(Math.random()*7);
		Piece piece=null;
		if(random==0){
			piece=new LeftL(this,game);
		}else if(random==1){
			piece=new RightL(this,game);
		}else if (random==2){
			piece=new T(this,game);
		}else if(random==3){
			piece=new Square(this,game);
		}else if(random==4){
			piece=new Stick(this,game);
		}else if(random==5){
			piece=new RightZ(this,game);
		}else if(random==6){
				piece=new LeftZ(this,game);
			}
		piece.fillBlocks();
		pieces.add(piece);
		
	}
	
	public boolean isValid(Location loc){
		if(loc.getRow()>=length-1){
			return false;
		}
		return true;
	}
	public boolean isOccupied(Location loc){
		for(int i=0;i<pieces.size()-1;i++){
			Piece p=pieces.get(i);
			ArrayList<Block> temp=p.getBlocks();
			for(Block b:temp){
				if(b.getLoc().getRow()==loc.getRow()&&b.getLoc().getCol()==loc.getCol()){
					System.out.println("occupied"+loc);
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return true;

				}
			}
		}
		return false;
	}
}